package com.citycloud.datac.client.client;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.citycloud.datac.client.cache.Cache;
import com.citycloud.datac.client.cache.CacheManager;
import com.citycloud.datac.client.constants.ClientInfo;
import com.citycloud.datac.client.dto.DetailedUserInfo;
import com.citycloud.datac.client.resoult.ResultObject;
import com.citycloud.datac.client.token.ResourceServerTokenServices;

import lombok.extern.slf4j.Slf4j;

/**
 * 城云认证器 依赖Oauth2 使用者可自主调用，用户获取城云认证中心的用户信息和token 不参与权限控制
 */
@Slf4j
public class DataCOauth2Client {
	CacheManager cacheManager;
	ResourceServerTokenServices tokenService;

	public Map<String, Object> checkToken(String token) {
		String key = "token:" + token;
		// 检查缓存
		Cache<String, Map<String, Object>> cache = cacheManager.getCache(ClientInfo.CACHE_NAME);
		Map<String, Object> authInfo = cache.get(key);
		log.info("缓存authInfo:{}", authInfo);
		if (authInfo == null) {
			log.info("进入encache缓存-----");
			authInfo = (Map<String, Object>) tokenService.checkToken(token);
			if (authInfo == null) {
                throw new RuntimeException("check_token失敗");
            }
			key = "token:" + authInfo.get("access_token");
			cache.put(key, authInfo);
			return authInfo;
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = df.parse(String.valueOf(authInfo.get("exp")));
			log.info("encache缓存有效期-----date:{}", date);
			if (new Date().compareTo(date) >= 0) {
				log.info("encache缓存过期了-----date:{},key:{}", date, key);
				cache.remove(key);
				authInfo = (Map<String, Object>) tokenService.checkToken(token);
				key = "token:" + authInfo.get("access_token");
				log.info("新的key:{}", key);
				cache.put(key, authInfo);
				return authInfo;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return authInfo;
	}

	public DetailedUserInfo getUserInfo(String token) {
		String key = "userInfo:" + token;
		// 检查缓存
		Cache<String, DetailedUserInfo> cache = cacheManager.getCache(ClientInfo.CACHE_NAME);
		DetailedUserInfo userInfo = cache.get(key);
		log.info("缓存userInfo:{}", userInfo);
		if (userInfo == null) {
			userInfo = tokenService.getUserInfo(token);
			if (userInfo != null) {
				cache.put(key, userInfo);
				return userInfo;
			}
		}
		return userInfo;

	}

	public String logout() {
		return tokenService.logout();
	}

	public ResultObject getToken(String code) {
		// 检查缓存
		ResultObject map = tokenService.getToken(code);
		/*
		 * String key = "token:" + map.get("access_token"); Cache<String, Map<String,
		 * Object>> cache = cacheManager.getCache(ClientInfo.CACHE_NAME); cache.put(key,
		 * map);
		 */
		return map;
	}

	public ResultObject getToken(String userName, String password) {
		// 检查缓存
		ResultObject map = tokenService.getToken(userName, password);
		/*
		 * String key = "token:" + map.get("access_token"); Cache<String, Map<String,
		 * Object>> cache = cacheManager.getCache(ClientInfo.CACHE_NAME); cache.put(key,
		 * map);
		 */
		return map;
	}

	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}

	public void setTokenService(ResourceServerTokenServices tokenService) {
		this.tokenService = tokenService;
	}
}
